﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="old.aspx.cs" Inherits="OpenRAMS.Community.oldBookings" %>
<%@ Import Namespace="OpenRAMS.Model" %>

<script runat="server">
    void backNow_Click(Object sender, EventArgs e)
    {
        Response.Redirect("Community.aspx");
    }      
      
</script>

<!DOCTYPE html />
<html>
<head runat="server">
    <title>OpenRAMS - Booking Archive</title>
    <link rel="Stylesheet" type="text/css" href="../Include/default.css" />
    <meta http-equiv="refresh" content="180" />
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="backNow" Text="<--- Back" OnClick="backNow_Click" runat="server" Width="15%"/>
    <table width="100%" class="tableDefault">
        <tr><td align="center" class="tableTitle">Archived Bookings</td></tr>
        <tr><td class="tableBody">
        <table width="100%">
        <tr class="listTop">
        <td><b>ID</b></td>
        <td><b>Title</b></td>
        <td><b>Description</b></td>
        <td><b>Location(s)</b></td>
        <td><b>Start</b></td>
        <td><b>End</b></td>
        <td><b>Resource(s)</b></td>
        <td><b>Assigned to</b></td>
        <td><b>Client</b></td>
        <td><b>Requester</b></td>
        <td><b>Status</b></td>
        <td><b>Options</b></td>
        </tr>
        <tr>
        <td colspan="13"><hr /></td>
        </tr>
        <%
          //for now, load all active bookings
            Bookings newBookingList = new Bookings();
            newBookingList.Populate("bitActive", "'false'");
            List<Booking> bookings = newBookingList.getBookingList();

            if (bookings.Count() > 0)
            {

                int loopCount = 0;
                
                //Order bookings by start date/time
                bookings.Sort(delegate(Booking a, Booking b)
                {
                    int x = a.bookStart.CompareTo(b.bookStart);
                    if (x != 0) return -x;  //just make this positive to reverse the order ;)
                    else return a.bookStart.CompareTo(b.bookStart);
                });
                
                foreach (Booking booking in bookings)
                {
                    //This seems like a lot of work to just get a row colour
                    //however, the user records are recycled later to output friendly usernames
                    UserRecord assignee = new UserRecord();
                    assignee.Load(booking.bookUserAssigned);
                    UserRecord creator = new UserRecord();
                    creator.Load(booking.bookCreator);
                    ClientRecord client = new ClientRecord();
                    client.Load(booking.bookClient);
                    
                    Response.Write("<tr>");
                    Response.Write("<td>" + booking.bookID + "</td>");
                    Response.Write("<td>" + booking.bookTitle + "</td>");
                    Response.Write("<td>" + booking.bookDesc + "</td>");
                    
                    //Grab rooms associated with booking
                    RoomRequests roomRequestList = new RoomRequests();
                    roomRequestList.Populate(booking.bookID);
                    List<RoomRequest> roomRequests = roomRequestList.getRoomRequests();

                    Response.Write("<td>");
                    if (roomRequests.Count() > 0)
                    {
                        int aLoopCount = 0;
                        foreach (RoomRequest roomRequest in roomRequests)
                        {
                            Response.Write(roomRequest.RoomName);
                            if (aLoopCount < roomRequests.Count() - 1)
                            {
                                Response.Write(", ");
                            }
                            aLoopCount++;
                        }
                    }
                    else
                    {
                        Response.Write("N/A");
                    }
                    Response.Write("</td>");

                    Response.Write("<td>" + booking.bookStart.ToString("ddd dd MMM yyyy HH:mm") + "</td>");
                    Response.Write("<td>" + booking.bookEnd.ToString("ddd dd MMM yyyy HH:mm") + "</td>");
                    
                    //Grab all resources associated with this booking
                    ResourceRequests resourceRequestList = new ResourceRequests();
                    resourceRequestList.Populate(booking.bookID);
                    List<ResourceRequest> resourceRequests = resourceRequestList.getResourceRequests();

                    Response.Write("<td>");
                    if (resourceRequests.Count() > 0)
                    {
                        int bLoopCount = 0;
                        foreach (ResourceRequest resourceRequest in resourceRequests)
                        {
                            Response.Write(resourceRequest.ResName);
                            if (bLoopCount < resourceRequests.Count() - 1)
                            {
                                Response.Write(", ");
                            }
                            bLoopCount++;
                        } 
                    }
                    else
                    {
                        Response.Write("N/A");
                    }
                    Response.Write("</td>");

                    Response.Write("<td>" + assignee.userFirstName + " " + assignee.userSecondName + "</td>");
                    Response.Write("<td>" + client.clientName + "</td>");
                    Response.Write("<td>" + creator.userFirstName + " " + creator.userSecondName + "</td>");
                    Response.Write("<td>" + booking.bookStatus + "</td>");    

                    Response.Write("<td>");
                    Response.Write("<a href=\"edit.aspx?bID=" + booking.bookID + "\">Edit</a> ");
                    Response.Write("<a href=\"revive.aspx?bID=" + booking.bookID + "\">Revive</a>");
                    Response.Write("</td>");
                                    
                    Response.Write("</tr>");
                    if (loopCount < bookings.Count() - 1)
                    {
                        Response.Write("<tr><td colspan=\"13\"><hr /></td></tr>");
                    }
                    loopCount++;
                }
                }
            
            else
            {
                Response.Write("<tr><td colspan=\"13\" align=\"center\">No active bookings present.</td></tr>");
            }           
            
            
        %>
        </table>
    </form>
</body>
</html>
